﻿@using Xunit;
@inherits TestContext
@code
{
    private string SamplePicture = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=";

    public FluentProfileMenuTests()
    {
        this.Services.AddScoped<GlobalState>();
        this.Services.AddSingleton(LibraryConfiguration.ForUnitTests);
        this.JSInterop.Mode = JSRuntimeMode.Loose;
    }

    [Fact]
    public void FluentProfileMenu_Default()
    {
        bool opened = false;

        // Arrange && Act
        var cut = Render(
            @<FluentProfileMenu Image="@SamplePicture"
                   ButtonSize="20px"
                   ImageSize="48px"
                   @bind-Open="@opened"
                   Status="@PresenceStatus.Available"
                   StatusTitle="I'm available"
                   HeaderLabel="Microsoft"
                   FooterLabel="v 3.14"
                   Initials="BG"
                   FullName="Bill Gates"
                   EMail="bill.gates@microsoft.com" />
    );

        Assert.False(opened);

        // Open the Popover
        var button = cut.Find(".initials");
        button.Click();

        // Assert
        Assert.True(opened);
        cut.Verify();
    }

    [Theory]
    [InlineData("Enter")]
    [InlineData(" ")]
    [InlineData("Spacebar")]
    [InlineData("NumpadEnter")]
    public void FluentProfileMenu_OpenWithKeyboard(string key)
    {
        bool opened = false;

        // Arrange && Act
        var cut = Render(
            @<FluentProfileMenu Image="@SamplePicture"
                   ButtonSize="20px"
                   ImageSize="48px"
                   @bind-Open="@opened"
                   Status="@PresenceStatus.Available"
                   StatusTitle="I'm available"
                   HeaderLabel="Microsoft"
                   FooterLabel="v 3.14"
                   Initials="BG"
                   FullName="Bill Gates"
                   EMail="bill.gates@microsoft.com" />
    );

        Assert.False(opened);

        // Open the Popover
        //var button = cut.Find(".initials");
        //button.Click();
        cut.Find(".fluent-profile-menu").KeyDown(key);

        // Assert
        Assert.True(opened);

    }

    [Fact]
    public void FluentProfileMenu_Buttons()
    {
        bool viewAccountClicked = false;
        bool signOutClicked = false;

        // Arrange && Act
        var cut = Render(
            @<FluentProfileMenu ButtonSize="20px"
                   HeaderButton="Log off"
                   FooterLink="Account"
                   OnHeaderButtonClick="@(e => signOutClicked = true)"
                   OnFooterLinkClick="@(e => viewAccountClicked = true)" />
        );

        // Open the Popover
        var button = cut.Find(".fluent-profile-menu");
        button.Click();

        // sign out
        var signOut = cut.Find("fluent-button");
        signOut.Click();

        // viewAccount
        var viewAccount = cut.Find("fluent-anchor");
        viewAccount.Click();

        // Assert
        Assert.True(viewAccountClicked);
        Assert.True(signOutClicked);
        Assert.Equal("Log off", signOut.InnerHtml);
        Assert.Equal("Account", viewAccount.InnerHtml);
    }

    [Fact]
    public void FluentProfileMenu_Customized()
    {
        // Arrange && Act
        var cut = Render(
    @<FluentProfileMenu Initials="DV">
        <HeaderTemplate>Header</HeaderTemplate>
        <ChildContent>Body</ChildContent>
        <FooterTemplate>Footer</FooterTemplate>
    </FluentProfileMenu>);

        // Open the Popover
        var button = cut.Find(".initials");
        button.Click();

        // Assert
        cut.Verify();
    }

    [Fact]
    public void FluentProfileMenu_StartEndTemplate()
    {
        // Arrange && Act
        var cut = Render(
    @<FluentProfileMenu Image="@SamplePicture">
        <StartTemplate>Before</StartTemplate>
        <EndTemplate>After</EndTemplate>
    </FluentProfileMenu>);

        // Assert
        cut.Verify();
    }

    [Fact]
    public void FluentProfileMenu_OpenTrue()
    {
        var opened = true;

        // Arrange
        var cut = Render(@<FluentProfileMenu Open="@opened" />).FindComponent<FluentProfileMenu>();

        // Assert: Opened
        Assert.Contains("fluent-anchored-region", cut.Markup);

        // Assert: Closed
        opened = false;
        cut.SetParametersAndRender(parameters => parameters.Add(p => p.Open, opened));

        Assert.DoesNotContain("fluent-anchored-region", cut.Markup);
    }
}
